<template>
    <div class="card">
        <el-input style="width: 240px;" v-model="data.searchname" placeholder="请输入员工姓名查询"
            prefix-icon="Search"></el-input>
        <el-button style="margin-left: 10px;" type="primary">查询</el-button>
        <el-button style="margin-left: 10px;">重置</el-button>
    </div>
    <div class="card">
        <el-table :data="data.employeelist" stripe>
            <el-table-column label="员工姓名" prop="employeeName" />
            <el-table-column label="员工年龄" prop="employeeAge" />
            <el-table-column label="职位" prop="position" :filters="[
                { text: '普通员工', value: '普通员工' },
                { text: '小组长', value: '小组长' },
                { text: '主管', value: '主管' },
                { text: '部长', value: '部长' },
                { text: '经理', value: '经理' },
                { text: '总经理', value: '总经理' },
            ]" :filter-method="filterHandler" />
            <el-table-column label="退休金" prop="employeePension" />
        </el-table>
        <div style="margin-top: 15px;">
            <el-pagination v-model:current-page="data.pageNumber" v-model:page-size="data.pageSize"
                :page-sizes="[1, 5, 10, 15, 20]" layout="total, sizes, prev, pager, next, jumper" :total="data.total"
                @size-change="load()" @current-change="load()" />
        </div>
    </div>
</template>
<script setup>
import request from '@/utils/request';
import { reactive } from 'vue';

const data = reactive({
    pageNumber: 1,
    pageSize: 5,
    total: 100,
    employeelist: [],
})
const load = () => {//加载所有退休员工
    request.get('/employee/searchPage', {
        params: {
            pageNumber: data.pageNumber,
            pageSize: data.pageSize,
            employeeName: data.searchname,
            enterpriseId: JSON.parse(localStorage.getItem('enterprise')).enterpriseId,
            isleave: 1,//退休员工    
        }
    }).then(res => {
        for (let i = 0; i < res.data.list.length; i++) {
            const birthDate = new Date(res.data.list[i].employeeAge); // 员工出生日期
            const today = new Date(); // 当前日期
            let age = today.getFullYear() - birthDate.getFullYear(); // 计算年份差
            // 如果当前月份和日期小于出生月份和日期，年龄减 1
            const monthDiff = today.getMonth() - birthDate.getMonth();
            const dayDiff = today.getDate() - birthDate.getDate();
            if (monthDiff < 0 || (monthDiff === 0 && dayDiff < 0)) {
                age--;
            }
            res.data.list[i].employeeAge = age; // 将计算的年龄赋值给 employeeAge
        }
        data.employeelist = res.data.list
        data.total = res.data.total
    })
}
load()
const filterHandler = (value, row, column) => {//通过职位筛选
    const property = column['property']
    return row[property] === value
}
</script>

<style scoped></style>